package com.appshelves;

import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;

import com.appshelves.utils.Utils;

public class Settings {

    private static final float DEFAULT_CELL_W_INCH = .29f;
    private static final float DEFAULT_CELL_H_INCH = .3f;
    private static final float DEFAULT_CELL_XMARGIN_INCH = 0.024f;
    private static final float DEFAULT_CELL_YMARGIN_INCH = 0.03f;
    private static final float DEFAULT_FONT_SIZE_INCH = 0.055f;
    private static final float DEFAULT_TEXT_MARIN_INCH = 0.00625f;

    /// 
    public static int marginHor;
    public static int marginVer;
    public static int shelvesNumber, shelfSize;
    public static float textSize;
    public static int textHeight, textBottom;
    public static int textMargin;

    /// these are calculated
    public static int iconWidth, iconHeight;
    private static int cellContentHeight;
    public static int cellWidth, cellHeight;
    public static int paddingLeft, paddingTop, paddingRight, paddingBottom;

    public static int getCellContentWidth() {
        return iconWidth;
    }

    public static int getCellContentHeight() {
        return cellContentHeight;
    }

    public static float getCaptionSize() {
        return Settings.cellWidth / 2.5f;
    }

    /// delete button

    public static int deleteButtonW() {
        return cellWidth * 3 / 4;
    }

    public static int deleteButtonH() {
        return cellHeight * 3 / 4;
    }

    public static int deleteButtonImageW() {
        return cellWidth / 4;
    }

    public static int deletButtonImageH() {
        return cellHeight / 4;
    }

    ///

    public static int draggableWidth() {
        return cellWidth * 3 / 2;
    }

    public static int draggableHeight() {
        return cellHeight * 3 / 2;
    }

    public static float dragHotSpotSize() {
        return cellHeight / 3;
    }

    /// initialization

    public static void init() {
        initDefaultParameters();

        //        AppList.eraseAppData();
        Display display = getDisplay();
        Point size = new Point();
        display.getSize(size);

        Paint p = new Paint();
        p.setTextSize(textSize);
        FontMetricsInt metrics = p.getFontMetricsInt();
        textHeight = metrics.bottom - metrics.top;
        textBottom = metrics.bottom;

        cellWidth = calcSellSize(size.x, shelfSize);
        cellHeight = calcSellSize(size.y, shelvesNumber);

        iconWidth = cellWidth - marginHor * 2;
        cellContentHeight = cellHeight - marginVer * 2;
        iconHeight = cellContentHeight - textHeight - textMargin;

        int paddingX = calcPadding(size.x, cellWidth, shelfSize);
        paddingLeft = paddingRight = paddingX;
        int paddingY = calcPadding(size.y, cellHeight, shelvesNumber);
        paddingTop = paddingBottom = paddingY;
    }

    private static void initDefaultParameters() {
        DisplayMetrics dm = getDisplayMetrics();

        float screenWInches = dm.widthPixels / dm.xdpi;
        float screenHInches = dm.heightPixels / dm.ydpi;
        shelvesNumber = (int) (screenHInches / DEFAULT_CELL_H_INCH);
        shelfSize = (int) (screenWInches / DEFAULT_CELL_W_INCH);

        marginHor = (int) (dm.xdpi * DEFAULT_CELL_XMARGIN_INCH);
        marginVer = (int) (dm.ydpi * DEFAULT_CELL_YMARGIN_INCH);

        textSize = dm.xdpi * DEFAULT_FONT_SIZE_INCH;
        textMargin = (int) (dm.ydpi * DEFAULT_TEXT_MARIN_INCH);
    }

    private static DisplayMetrics getDisplayMetrics() {
        Display display = getDisplay();
        DisplayMetrics dm = new DisplayMetrics();
        display.getMetrics(dm);
        return dm;
    }

    private static Display getDisplay() {
        return Utils.getActivity().getWindowManager().getDefaultDisplay();
    }

    private static int calcSellSize(int size, int number) {
        return size / number;
    }

    private static int calcPadding(int size, int cellSize, int cellsNumber) {
        return (size - cellSize * cellsNumber + 1) / 2;
    }
}
